import { useNavigate } from 'react-router-dom';
import { useEffect, useState } from 'react';
import '@/style/news.scss'
import { Pagination } from 'antd';
function News() {
	const navigate = useNavigate();
	const [newsData, setNewsData] = useState([]);
	const [current, setCurrent] = useState(0);
	const [total, setTotal] = useState(0);
	const [sid, setSid] = useState('');
	const href = window.location.href;
	const typeName = href.split('?type=') ? href.split('?type=')[1] : '';

	const getData = async () => {
		try {
			const pageObj = {
				size: 8,
				start: current,
				moduleId:"c1e5601c-5974-46f7-b27f-b0ce8b7f892a"
			}
			const res = "";
			if (res.result === 'ok' && res.data) {
				const newArr = res.data.messageList.map(item => {
					const titlePic = item?.titPicList[0]?.titPicUrl
					console.log('titlePic', titlePic)
					const newTitlePic = titlePic?.replace(new RegExp('./df', 'g'), '/portal/r/df')
					return {
						...item,
						titlePicUrl: newTitlePic
					}
				})
				setNewsData(newArr || [])
				setTotal(res.data.counts);
			}
		} catch (error) {
			console.error(error);
		}
	};
	const openDetail = (item) => {
		navigate('/news/detail', {
			state: item
		})
	}
	const onPageChange = (pageNumber) => {
		setCurrent(pageNumber)
	};
	const scroll = (typeName) => {
		let element = document.getElementById(typeName)
		element.scrollIntoView({ block: 'center', behavior: 'smooth' })
	}
	useEffect(() => {
		if (typeName) {
			scroll(typeName)
		}
	}, []);
	useEffect(() => {
		if (sid) {
			getData();
		}
	}, [sid, current]);
	
	return (
		<>
			<div className="newsBanner">
			</div> 
			<div className="newsBox" id="news">
				<div className="mainContent">
					{newsData.map((item, index) => {
						return (
							<div 
								className="newsItem"
								key={index}
								onClick={openDetail.bind(null, item)}>
								<img src={item.titlePicUrl}></img>
								<div className="newsItemTitle">
									{item.msgTitle}
								</div>
								<div className="newsItemContent"  dangerouslySetInnerHTML={{ __html: item.msgSummary }}></div>
								<div className="newsItemTime">
									{item.releaseTime}
								</div>
							</div>
						)
					})}
				</div>
				{total > 8 && (
					<div className="mainContent">
						<div className="pageBox">
							<Pagination defaultCurrent={current} total={total} onChange={onPageChange}/>
						</div>
					</div>
				)}
				{!newsData.length && (
					<div className="noDataBox">
						<i></i>
						<p>暂无动态</p>
					</div>
				)}
			</div> 
		</>
		
	)
}
 
export default News